package cn.edu.neusoft.cqy917.zyfypt_cqy917.model;

import java.util.List;

import cn.edu.neusoft.cqy917.zyfypt_cqy917.bean.ProjectBean;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.ProjectListener;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.iface.Projectiface;
import cn.edu.neusoft.cqy917.zyfypt_cqy917.service.ProjectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;



public class ProjectModel implements Projectiface {
    private ProjectService projectService;
    private Retrofit retrofit;
    private static final String BASE_URL = "http://amicool.neusoft.edu.cn/";
    public ProjectModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final ProjectListener projectListener) {
        projectService = retrofit.create(ProjectService.class);
        Call<List<ProjectBean>> call = projectService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<ProjectBean>>() {
            @Override
            public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                    projectListener.onResponse(response.body());
                else
                    projectListener.onFail("error");
            }

            @Override
            public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                projectListener.onFail("error");
            }
        });
    }
}
